package com.zthz.douxing.sendmessageservice.utils

import com.zthz.douxing.MessageToUserLog
import com.zthz.douxing.TripSegment
import com.zthz.douxing.WeatherType
import com.zthz.douxing.event.UserWillDepartEvent
import com.zthz.douxing.msg.MessageType
import com.zthz.douxing.util.CommonUtilService
import com.zthz.douxing.utils.FetchDataUtils
import groovy.time.TimeCategory
import groovy.time.TimeDuration

class OrderXingeMessageService {

    def commonUtilService

   public String orderMessage(UserWillDepartEvent userWillTripEvent){
       TripSegment tripSegment = userWillTripEvent.tripSegment
       String planTripInfo = getPlanAlsert(tripSegment)
       return planTripInfo
   }


    public String orderMesageArriveStatus(UserWillDepartEvent userWillTripEvent){
        TripSegment tripSegment = userWillTripEvent.tripSegment
        String planTripInfo = getLPlanArriveAlert(tripSegment)
        return planTripInfo
    }

    public  String orderWeatherMessage(UserWillDepartEvent userWillTripEvent){
        String weather = commonUtilService.getDateFromRedis(
            FetchDataUtils.getCambinDataKey(userWillTripEvent))
        String weatherInfo = ""

        if(weather){
            def jsonWeathor = commonUtilService.getDataFromJson(weather)
            String weatherDesc =  WeatherType
                    .findByWeatherTypeNight(
                    jsonWeathor.get("weather_type_day"))?.weatherTypeDay
            if(jsonWeathor?.temperature){
                  String [] temps = ((String)(jsonWeathor.temperature)).split("/")
                if(temps?.size()>0){
                    if(temps[0]?.startsWith("-")){
                        weatherDesc+="，最高温度：零下"+temps[0]
                    }else{
                        weatherDesc+="最高温度："+temps[0]
                    }

                    weatherDesc+="，"
                    if(temps[1]?.startsWith("-")){
                        weatherDesc+="最低温度：零下"+temps[1]
                    }else
                        weatherDesc+="最低温度："+temps[1]
                }
            }

            weatherInfo = getWeatherAlsert(userWillTripEvent.tripSegment,weatherDesc)
        }
        return weatherInfo
    }



    private String getPlanAlsert(TripSegment content){
//        beginPlanStatus=
// 您将乘坐的${date} ${startCity}
// 开往 ${arriveCity}的 ${trainNo} 列车 距开车时间约有${slupTime}，请您提前到达${arriveStation}。

        int house,minutes
        Date endTime = content.getStartTime()
        use(TimeCategory) {
            TimeDuration surplus = TimeCategory.minus(endTime,new Date())
            house = surplus.getHours()
            minutes = surplus.getMinutes()

        }
        HashMap para = new HashMap()
        para.put("date",getFormateStr(content.startTime))
        para.put("startCity",content.startCity)
        para.put("arriveCity",content.arriveCity)
        para.put("trainNo",content.trainNo)
        para.put("slupTime",house+"小时"+minutes+"分钟")
        para.put("arriveStation",content.startStation)

        return commonUtilService.getTemplateString("beginPlanStatus",para)
    }
    private String getLPlanArriveAlert(TripSegment content){

        //        您乘坐的${trianNo}列车前方即将到达${arriveStation}，
//          请您提前做好下车前准备，别忘带个人物品。
//
        HashMap para = new HashMap()
        para.put("trianNo",content.trainNo)
        para.put("arriveStation",content.arriveStation)

        return commonUtilService.getTemplateString("arrivePlanStatus",para)

    }
    /**
     * 获取天气提示信息
     * */
    private getWeatherAlsert(TripSegment content,String weatherType){
        HashMap para = new HashMap()
        //您将在${date}前往的${city}天气情况：${temp}。请您提前做好准备。
        para.put("date",getFormateStr(content.getArriveTime()))
        para.put("city",content.arriveCity)
        para.put("temp",weatherType)
        return commonUtilService.getTemplateString("weatherTemplate",para)
    }

    private String getFormateStr(Date _date){
        Calendar cal= Calendar.getInstance()
        cal.setTime(_date)
        return new StringBuffer().append(""+cal.get(Calendar.YEAR)+"年")
                .append(""+(cal.get(Calendar.MONTH)+1)+"月")
                .append(""+cal.get(Calendar.DAY_OF_MONTH)+"日")
                .append(""+cal.get(Calendar.HOUR_OF_DAY)+"时")
                .append(""+cal.get(Calendar.MINUTE)+"分")
                .toString()
    }

    public void saveMessageToUserLog(UserWillDepartEvent userWillTripEvent,
                                     String msgContent,String resultDesc,
                                     MessageType messageType){
       if(userWillTripEvent.tripSegment && userWillTripEvent.tripSegment.id){
           MessageToUserLog messageToUserLog = new MessageToUserLog()
           messageToUserLog.messageContext = msgContent
           messageToUserLog.sendTime = new Date()
           messageToUserLog.tripSegmentId = userWillTripEvent?.tripSegment?.id
           messageToUserLog.messageResultDesc =resultDesc
           messageToUserLog.type = messageType
           messageToUserLog.save()
       }
    }
    public void saveMessageToUserLog(Long tripId,
                                     String msgContent,String resultDesc,
                                     MessageType messageType){
        MessageToUserLog messageToUserLog = new MessageToUserLog()
        messageToUserLog.messageContext = msgContent
        messageToUserLog.sendTime = new Date()
        messageToUserLog.tripSegmentId = tripId
        messageToUserLog.messageResultDesc =resultDesc
        messageToUserLog.type = messageType
        messageToUserLog.save()
    }
}